package com.ruoyi.function.service.impl;

import java.math.BigDecimal;
import java.util.List;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.function.constant.BusinessConstants;
import com.ruoyi.function.service.ITransferResultService;
import com.ruoyi.function.service.IUserFlowingService;
import com.ruoyi.message.controller.NoticeController;
import com.ruoyi.message.notice.vo.NoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.function.mapper.AuditMapper;
import com.ruoyi.function.domain.Audit;
import com.ruoyi.function.service.IAuditService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Null;

/**
 * 提现审核Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-20
 */
@Service
public class AuditServiceImpl implements IAuditService {
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private IUserFlowingService iUserFlowingService;
    @Autowired
    private ITransferResultService transferResultService;


    @Autowired
    private NoticeController noticeController;
    /**
     * 查询提现审核
     *
     * @param auditId 提现审核主键
     * @return 提现审核
     */
    @Override
    public Audit selectAuditByAuditId(Long auditId) {
        return auditMapper.selectAuditByAuditId(auditId);
    }

    /**
     * 查询提现审核列表
     *
     * @param audit 提现审核
     * @return 提现审核
     */
    @Override
    public List<Audit> selectAuditList(Audit audit) {
        return auditMapper.selectAuditList(audit);
    }

    /**
     * 新增提现审核
     *
     * @param audit 提现审核
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAudit(Audit audit) {
        //提现必须大于0
        if (audit.getBalance().compareTo(BigDecimal.ZERO) <= 0) {
            throw new RuntimeException("提现金额必须大于0");
        }
        audit.setUserId(SecurityUtils.getUserId());
        audit.setCreateBy(SecurityUtils.getUserId() + "");
        audit.setCreateTime(DateUtils.getNowDate());
        audit.setStatus(BusinessConstants.dict.audit_status_1);
        iUserFlowingService.addAmount(SecurityUtils.getUserId(), mul(audit.getBalance(), -1), BusinessConstants.dict.fee_type_2, audit.getUserTypes(), null);
        return auditMapper.insertAudit(audit);
    }

    /**
     * 修改提现审核
     *
     * @param audit 提现审核
     * @return 结果
     */
    @Override
    public int updateAudit(Audit audit) {
        audit.setUpdateTime(DateUtils.getNowDate());
        audit.setStatus(BusinessConstants.dict.audit_status_1);
        return auditMapper.updateAudit(audit);
    }

    /**
     * 批量删除提现审核
     *
     * @param auditIds 需要删除的提现审核主键
     * @return 结果
     */
    @Override
    public int deleteAuditByAuditIds(Long[] auditIds) {
        return auditMapper.deleteAuditByAuditIds(auditIds);
    }

    /**
     * 删除提现审核信息
     *
     * @param auditId 提现审核主键
     * @return 结果
     */
    @Override
    public int deleteAuditByAuditId(Long auditId) {
        return auditMapper.deleteAuditByAuditId(auditId);
    }

    @Override
    @Transactional
    public int check(Audit audit) {

        Audit audit1 = auditMapper.selectAuditByAuditId(audit.getAuditId());
        if (!BusinessConstants.dict.audit_status_1.equals(audit1.getStatus())) {
            //报错
            throw new RuntimeException("只有待审核状态可以审核");
        }
        Audit audit2 = new Audit();
        audit2.setAuditId(audit.getAuditId());
        audit2.setStatus(audit.getStatus());
        audit2.setReason(audit.getReason());
        audit2.setUpdateTime(DateUtils.getNowDate());
        audit2.setUpdateBy(SecurityUtils.getUserId() + "");
        int i = auditMapper.updateAudit(audit2);
        if (BusinessConstants.dict.audit_status_2 == audit.getStatus()) {
            //微信进行打款
            transferResultService.weChatPaymentMake(audit1.getUserId(), audit1.getBalance(), audit1.getAuditId(), audit1.getUserTypes());
        } else if (BusinessConstants.dict.audit_status_3 == audit.getStatus()) {
            //发送消息
            NoticeVO noticeVO = new NoticeVO();
            noticeVO.setReceiverId(audit.getUserId() + "");
            noticeVO.setMessageContent(BusinessConstants.message_2);
            noticeVO.setUserType(audit.getUserTypes());
            noticeController.add(noticeVO);
            iUserFlowingService.addAmount(audit.getUserId(), audit.getBalance(), BusinessConstants.dict.fee_type_3, audit.getUserTypes(), null);
        }

        return i;
    }




    /**
     * 精确乘法运算
     * @param num1 被乘数（支持所有Number子类）
     * @param num2 乘数（支持所有Number子类）
     * @return 乘法结果的BigDecimal对象，输入为null时返回null
     */
    public BigDecimal mul(Number num1, Number num2) {
        if (num1 == null || num2 == null) {
            return null;
        }
        return new BigDecimal(num1.toString()).multiply(new BigDecimal(num2.toString()));
    }


}
